package nadav.tasher.lightool.graphics.view.appview.corner;

import nadav.tasher.lightool.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;
import java.util.ArrayList;

public class Corner extends DirectionalLayout {
    public static final int TOP_LEFT = 0;
    public static final int TOP_RIGHT = 1;
    public static final int BOTTOM_LEFT = 2;
    public static final int BOTTOM_RIGHT = 3;

    private int
            size = 256,
            radii = size,
            alpha = 128,
            color = -1,
            location = TOP_LEFT;

    private ArrayList<OnState> onstates = new ArrayList<>();
    private boolean isOpened = false;

    public Corner(Context context, int size, int color) {
        super(context);
        this.size = size;
        this.color = color;
        init();
    }

    private void init() {
        super.removeAllComponents();
        setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                isOpened = !isOpened;
                if (isOpened){
                    for (int a=0; a<onstates.size(); a++){
                        if (onstates.get(a) != null) onstates.get(a).onOpen();
                    }
                } else {
                    for (int a=0; a<onstates.size();a++){
                        if (onstates.get(a) != null) onstates.get(a).onClose();
                    }
                }
                for (int a=0; a<onstates.size(); a++){
                    if (onstates.get(a) != null) onstates.get(a).onBoth(isOpened);
                }
            }
        });
        initSize();
        setColor(color);
    }

    private void initSize(){
        setLayoutConfig(new LayoutConfig(size,size));
        setRadii(size);
    }

    private void setRadii(int rad){
        radii = rad;
        corner();
    }

    public void setView(Component c,double percent){
        super.removeAllComponents();
        if (c != null){
            c.setLayoutConfig(new LayoutConfig((int)(size*percent),(int)(size*percent)));
            super.addComponent(c);
        }
    }

    public void setColorAlpha(int a) {
        alpha = a;
        corner();
    }

    private void corner(){
        float[] rad=new float[]{
                radii * value(location, BOTTOM_RIGHT),
                radii * value(location, BOTTOM_RIGHT),
                radii * value(location, BOTTOM_LEFT),
                radii * value(location, BOTTOM_LEFT),
                radii * value(location, TOP_LEFT),
                radii * value(location, TOP_LEFT),
                radii * value(location, TOP_RIGHT),
                radii * value(location, TOP_RIGHT)
        };
        ShapeElement oval=new ShapeElement();
        //oval.setShape(size);
        oval.setBounds(0, 0, size, size);
        oval.setCornerRadiiArray(rad);
        oval.setRgbColor(RgbColor.fromArgbInt(color));
        setBackground(oval);
    }

    private int value(int corner, int wanted){
        if (corner == wanted) return 1;
        return 0;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
        initSize();
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color){
        this.color = color;
        this.color = Color.argb(alpha,RgbColor.fromArgbInt(color).getRed(),RgbColor.fromArgbInt(color).getGreen(), RgbColor.fromArgbInt(color).getBlue());
        corner();
    }

    public void setState(boolean state) {
        isOpened = state;
    }

    public void setLocation(int location){
        this.location=location;
        int g;
        System.out.println("Start="+location);
        if (location == TOP_RIGHT || location == TOP_LEFT){
            g = LayoutAlignment.TOP;
        } else {
            g = LayoutAlignment.BOTTOM;
        }
        if (location == TOP_LEFT || location == BOTTOM_LEFT){
            g = g | LayoutAlignment.START;
        }else {
            g = g | LayoutAlignment.END;
        }
        setAlignment(g);
        corner();
        System.out.println("//////////////g="+g);
    }
    public void addOnState(OnState onState){
        onstates.add(onState);
    }
    public void removeOnState(OnState onState) {
        onstates.remove(onState);
    }

    public void removeAllOnStates() {
        onstates.clear();
    }

    public interface OnState {
        void onOpen();

        void onClose();

        void onBoth(boolean isOpened);
    }
}
